package com.helloworld.demo.atomic;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.locks.LockSupport;

public class ABATest {
    public static void main(String[] args) {

        AtomicInteger atomicInteger = new AtomicInteger(1);


        new Thread(new Runnable() {
            @Override
            public void run() {
                int value = atomicInteger.get();
                System.out.println("Thread1 read value:" + value);

                //阻塞1s
                LockSupport.parkNanos(1000000000L);

                if(atomicInteger.compareAndSet(value,3)){
                    System.out.println("Thread1 update from" + value + "to 3");
                }else{
                    System.out.println("Thread1 update fail!");
                }
            }
        },"Thread-me").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int value = atomicInteger.get();
                System.out.println("Thread2 read value：" + value);

                if(atomicInteger.compareAndSet(value,2)){
                    System.out.println("Thread2 update form" + value + "to 2");

                    value = atomicInteger.get();
                    System.out.println("Thread2 value:" + value);

                    if(atomicInteger.compareAndSet(value,1)){
                        System.out.println("Thread2 update from" + value + "to 1");
                    }

                }
            }
        },"Thread-me2").start();

    }
}
